package com.photoeditor.demo.model.image.collage.templet;

/**
 * 定义一条直线
 * y = kx + b;
 */
public class Line {

    private float mK;
    private float mB;

    private boolean mHrizontal = false;
    private boolean mVertical = false;

    //竖直的时候 mDefaultX不变
    private float mDefaultX;

    //水平的时候 mDefaultY不变
    private float mDefaultY;

    //普通直线
    public Line(float k, float b){
        mK = k;
        mB = b;
    }

    /**
     * 特殊直线的构造方法
     * @param hrizontal
     * @param vertical
     */
    public Line(boolean hrizontal, boolean vertical, float x, float y){
        mHrizontal = hrizontal;
        mVertical = vertical;
        if(mHrizontal){
            mDefaultY = y;
        } else if(mVertical){
            mDefaultX = x;
        }
    }

    /**
     *
     * @param x1
     * @param y1
     * @param x2
     * @param y2
     */
    public Line (float x1, float y1, float x2, float y2){
        if(y1 == y2){
            mHrizontal = true;
            mDefaultY = y1;
        } else if(x1 == x2){
            mVertical = true;
            mDefaultX = x1;
        } else {
            mK = (y2 - y1) / (x2 - x1);
            mB = y1 - mK * x1;
        }
    }

    public float getK() {
        return mK;
    }


    public float getB() {
        return mB;
    }

    public boolean isHrizontal(){
        return mHrizontal;
    }

    public boolean isVertical(){
        return mVertical;
    }

    public float getY(float x){
        float y = 0;
        if(mHrizontal){
            y = mDefaultY;
        } else if(mVertical){
            y = 0;
        } else{
            y = mK * x + mB;
        }
        return y;
    }

    public float getX(float y){
        float x = 0;
        if(mHrizontal){
            x = 0;
        } else if(mVertical){
            x = mDefaultX;
        } else{
            x = (y - mB) / mK;
        }
        return x;
    }
}
